<?php
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Bill;
use App\Models\Recharge;
use App\Models\RechargeOrder;
use App\Models\User;
use App\Models\UserScoreRecord;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ScoreController extends Controller
{
    //积分列表
    public function scoreListApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $scoreCount = UserScoreRecord::where("status",2)->count();
            $scoreList = UserScoreRecord::where("status",2)->with("user")->skip(($page-1)*$size)->take($size)->get();
            return self::jsondata($scoreCount,$scoreList);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //充值记录列表
    public function scoreRechargePayApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $scoreCount = RechargeOrder::count();
            $scoreList = RechargeOrder::with("user")->skip(($page-1)*$size)->take($size)->get();
            return self::jsondata($scoreCount,$scoreList);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //积分审核历史记录
    public function scoreRecordsListApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $scoreCount = UserScoreRecord::count();
            $scoreList = UserScoreRecord::with("user")->skip(($page-1)*$size)->take($size)->orderBy('id','desc')->get();
            return self::jsondata($scoreCount,$scoreList);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
    
    //积分审核
    public function scoreEditApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);

            DB::beginTransaction();
            //修改审核状态
            $score = UserScoreRecord::find($id);
            $score->status = 1;
            $score->save();
            //增加用户积分
            $user = User::find($score->user_id);
            $user->score = $user->score+$score->score;
            $user->save();
            //增加消费记录
            $bill = new Bill();
            $bill->order_sn = $score->order_sn;
            $bill->userid = $score->user_id;
            $bill->money = $score->score;
            $bill->type = 1;
            $bill->status = 1;
            $bill->created_at = Carbon::now();
            $bill->save();
            DB::commit();
            return self::success();

        }catch (\Exception $e){
            Log::error($e);
            DB::rollBack();
            return self::error(500);
        }
    }

    //删除积分记录
    public function scoreRecordsDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $score = UserScoreRecord::find($id);
            $score->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }

    }

    //充值套餐列表
    public function rechargeListApi(){
        try{
            $rechargeData = Recharge::get();
            return self::jsondata(0,$rechargeData);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //新增充值套餐
    public function rechargeAddApi(Request $request){
        try{
            $money = $request->input("money",false);
            $score = $request->input("score",false);
            if (!$money || !$score)return self::error(1000);
            $recharge = new Recharge();
            $recharge->money = $money;
            $recharge->score = $score;
            $recharge->created_at = Carbon::now();
            $recharge->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //新增充值套餐
    public function rechargeEditApi(Request $request){
        try{
            $id = $request->input("id",false);
            $money = $request->input("money",false);
            $score = $request->input("score",false);
            if (!$id || !$money || !$score)return self::error(1000);
            $recharge = Recharge::find($id);
            $recharge->money = $money;
            $recharge->score = $score;
            $recharge->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除充值套餐接口
    public function rechargeDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $recharge = Recharge::find($id);
            $recharge->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}